{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 条件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "2\n",
      "真\n",
      "或\n"
     ]
    }
   ],
   "source": [
    "m = 2\n",
    "print(1<m<3)\n",
    "\n",
    "if m == 1: #注意有冒号。python没有{}，严格缩进，避免空格和制表符混用！\n",
    "    print(1)\n",
    "elif m == 2:\n",
    "    print(2)\n",
    "else:\n",
    "    print(3)\n",
    "\n",
    "a = False\n",
    "if not a: #与或非分别是and, or, not\n",
    "    print('真')\n",
    "b = True\n",
    "if a or b:\n",
    "    print('或')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 循环\n",
    "\n",
    "for循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 4 9 16 25 36 49 64 81 \n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print(i*i, end=' ')\n",
    "print()\n",
    "    \n",
    "a=[1,2,3,4,5]\n",
    "for i in a:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "while循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "sum = 0\n",
    "i=1\n",
    "while i <= 100:\n",
    "    sum += i\n",
    "    i += 1\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "字典循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(math,100)\n",
      "(physics,80)\n",
      "(computer,90)\n",
      "math 100\n",
      "physics 80\n",
      "computer 90\n"
     ]
    }
   ],
   "source": [
    "d={'math':100, 'physics': 80, 'computer':90}\n",
    "for k in d:\n",
    "    print('(%s,%d)' %(k,d[k]))\n",
    "\n",
    "for k,v in d.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "特殊循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sai: 28\n",
      "wang: 26\n",
      "li: 24\n",
      "0 sai\n",
      "1 wang\n",
      "2 li\n",
      "是素数\n"
     ]
    }
   ],
   "source": [
    "name = ['sai', 'wang', 'li']\n",
    "age = [28, 26, 24]\n",
    "for n,a in zip(name,age):\n",
    "    print('%s: %d' %(n,a))\n",
    "for i,v in enumerate(name):\n",
    "    print(i,v)\n",
    "\n",
    "n=37\n",
    "for i in range(2,n//2):\n",
    "    if n%i == 0:\n",
    "        print('是合数')\n",
    "        break\n",
    "else: #for-else为python独有，非常好用\n",
    "    print('是素数')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
